package ru.housewife.form;

import ru.housewife.model.IngredientGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Form object for
 * @see ru.housewife.model.IngredientGroup
 *
 * @author atroshanin
 */
public class IngredientGroupForm extends AbstractFormObject<IngredientGroupForm> implements Comparable<IngredientGroupForm> {

    private long id;

    private String name;

    private IngredientGroupForm parent;

    private List<IngredientGroupForm> childs;

    private List<IngredientForm> ingredients;

    private boolean ingredientsSorted;

    private boolean defaultGroup;

    private int order;

    public IngredientGroupForm(IngredientGroup ingredientGroup) {
        id = ingredientGroup.getId();
        name = ingredientGroup.getName();
        defaultGroup = ingredientGroup.isDefaultGroup();
        order = ingredientGroup.getOrder();

        childs = new ArrayList<IngredientGroupForm>(ingredientGroup.getChilds().size());
        for (IngredientGroup child : ingredientGroup.getChilds()) {
            IngredientGroupForm childForm = new IngredientGroupForm(child);
            childForm.setParent(this);
            childs.add(childForm);
        }
        Collections.sort(childs);
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IngredientGroupForm getParent() {
        return parent;
    }

    public void setParent(IngredientGroupForm parent) {
        this.parent = parent;
    }

    public List<IngredientGroupForm> getChilds() {
        return childs;
    }

    public void setChilds(List<IngredientGroupForm> childs) {
        this.childs = childs;
    }

    public List<IngredientForm> getIngredients() {
        if (ingredients == null) {
            return Collections.emptyList();
        } else if (!ingredientsSorted) {
            Collections.sort(ingredients);
            ingredientsSorted = true;
        }
        return ingredients;
    }

    public void setIngredients(List<IngredientForm> ingredients) {
        this.ingredients = ingredients;
    }

    public void addIngredient(IngredientForm ingredient) {
        if (ingredients == null) {
            ingredients = new ArrayList<IngredientForm>();
        }
        ingredients.add(ingredient);
    }

    public boolean isDefaultGroup() {
        return defaultGroup;
    }

    public void setDefaultGroup(boolean defaultGroup) {
        this.defaultGroup = defaultGroup;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    @Override
    public void updateFieldsFromSrcForm(IngredientGroupForm srcForm) {
        //Todo...
    }

    @Override
    public int compareTo(IngredientGroupForm that) {
        int thisVal = this.order;
        int thatVal = that.order;
        return (thisVal < thatVal ? -1 : (thisVal == thatVal ? 0 : 1));
    }
}
